<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="生成人数" prop="num">
          <el-input
            v-model="form.model.num"
            placeholder="请输入生成虚拟人数"
            type="number"
            min="0"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button type="primary" :loading="loading" :disabled="loading" @click="confirm"
        >确 定</el-button
      >
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../data.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      num: 1,
    },
    rules: {
      num: [{ required: true, message: '请输入生成虚拟人数', trigger: 'blur' }],
    },
  });
  const loading = ref(false);
  // 表单关闭时提交
  async function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      loading.value = true;
      form.model.num = Number(form.model.num);
      const { error } = await api.fakeUser.random(form.model);
      if (error == 0) {
        loading.value = false;
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
</script>
